package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStratrgyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IStratrgyStatisVORedisService statisVORedisService;

    @GetMapping("/detail")
    public Object strategyDetail(String id){

        Strategy strategy = strategyService.get(id);
        //每访问一次接口,阅读量加一
        statisVORedisService.viewnumIncrease(id,1);
        return JsonResult.success(strategy);
    }
    @GetMapping("/query")
    public Object query(StrategyQuery qo){

        return JsonResult.success(strategyService.query(qo));
    }
    @GetMapping("/themes")
    public Object strategyThemes(){

        return JsonResult.success(strategyThemeService.list());
    }

    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        //bean属性复制
        //前提:属性名字必须一致,且类型相同的时候才可以复制
        //操作原理,javabean内审机制(反射)
        BeanUtils.copyProperties(userInfo,comment);
        comment.setUserId(userInfo.getId());
        commentService.save(comment);
        //评论数+1
        statisVORedisService.replynumIncrease(comment.getStrategyId(),1);

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){

        return JsonResult.success(commentService.query(qo));
    }

    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){

        commentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){

        return JsonResult.success(statisVORedisService.getStrategyStatisVO(sid));
    }

    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        //true,表示存在,false表示不存在
        boolean ret = statisVORedisService.strategyFavor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        //true,表示存在,false表示不存在
        boolean ret = statisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    @GetMapping("hotStrategy")
    public Object queryHotStrategy(){

        return JsonResult.success(strategyService.queryHotStrategy());
    }

}
